﻿using System;
using System.Linq;
using CodeGenerator.interfaces;

namespace CodeGenerator.Implementations
{
    class Property : MultipleTargetObject, IProperty
    {
        private readonly string _propertyName;
        private readonly IDatabaseColumn _column;
        
        private IDataBaseTable _containerTable;

        public Property(string PropertyName, IDatabaseColumn Column, IDataBaseTable ContainerTable)
        {
            _propertyName = PropertyName;
            _containerTable = ContainerTable;
            _column = Column;
        }

        public IDataBaseTable ContainerTable
        {
            get { return _containerTable; }
        }

        public string PropertyName
        {
            get { return Common.Formatting.FormatPascalCase(_propertyName); }
        }

        public IDatabaseColumn DatabaseColumn
        {
            get { return _column; }
        }

        public string DataClassLocalVariableName
        {
            get { 
                string res = Common.Formatting.ToString_OneTwoThree(_propertyName);
                res = "_" + res.Substring(0, 1).ToLower() + res.Substring(1);
                return res;
            }
        }

        public bool IsPrimaryKey()
        {
            return (from x in _containerTable.PrimaryKey where x.ColumnName == this.DatabaseColumn.ColumnName select x).Count() > 0;
        }


        public int GetParameterPosition()
        {
            for (int i = 0; i < _containerTable.Columns.Count; i++)
            {
                if(_containerTable.Columns[i].ColumnName == _column.ColumnName)
                    return i;
            }
            throw new InvalidOperationException("Parameter Not found!!! Inconsistence detected!!!");
        }
    }
}